package com.zq.widget.inverted.game.role.impl.plane;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.animation.AnimationUtils;

import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.decorate.Decorator;
import com.zq.widget.inverted.game.role.decorate.HPBar;
import com.zq.widget.inverted.game.role.equipment.EquipmentRole;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.hp.HPProxy;
import com.zq.widget.inverted.game.role.impl.type.Enemy;

/**
 * Created by zhangqiang on 2018/4/2.
 */
public class Plane2 extends EquipmentRole implements HP,Damage,Enemy{

    private float startX;
    private final float startY;
    private float rotateVelocity;
    private float rotateRadius;
    private float rotateOffset;
    private float moveVelocity;
    private int moveDegree;
    private long startTime;
    private HP hpProxy;
    private Damage damageProxy;
    private Decorator decorator;

    public Plane2(Context context, int drawableResId, float startX, float startY, float rotateVelocity, float rotateRadius, float rotateOffset, float moveVelocity, int moveDegree) {
        super(context, drawableResId);
        this.rotateOffset = rotateOffset;
        this.startX = startX + rotateRadius;
        this.startY = startY;
        this.rotateVelocity = rotateVelocity;
        this.rotateRadius = rotateRadius;
        this.moveVelocity = moveVelocity;
        this.moveDegree = moveDegree;
        hpProxy = new HPProxy(20);
        damageProxy = new DamageProxy(10);
        updateLocation(startX,startY);
        decorator = new HPBar(1, Color.RED,10,5,hpProxy);
    }

    @Override
    public void preDraw() {
        super.preDraw();

        long timeMillis = AnimationUtils.currentAnimationTimeMillis();
        if (startTime <= 0) {
            startTime = timeMillis;
        }
        float pastTime = (float)(timeMillis - startTime) / 1000;

        float distance = moveVelocity * pastTime;
        double radians = Math.toRadians(moveDegree);
        float dy = (float) (distance * Math.sin(radians));
        float dx = (float) (distance * Math.cos(radians));

        float rotateDegree = pastTime * rotateVelocity + rotateOffset;
        double rotateRadian = Math.toRadians(rotateDegree);
        dy += rotateRadius * Math.sin(rotateRadian);
        dx += rotateRadius * Math.cos(rotateRadian);

        updateLocation(startX + dx, startY + dy);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        decorator.onDraw(canvas,bounds());
    }

    @Override
    public boolean isFinish() {
        return super.isFinish() || isBeyondCanvas() || getHp() <= 0;
    }

    @Override
    public long totalHp() {
        return hpProxy.totalHp();
    }

    @Override
    public long getHp() {
        return hpProxy.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpProxy.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpProxy.reduceHp(hp);
    }

    @Override
    public long damage() {
        return damageProxy.damage();
    }
}
